package com.ciko.code.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ciko.code.adapter.ResListLeaveAdapter;
import com.ciko.code.adapter.ResListOutAdapter;
import com.ciko.code.adapter.ResListOverWorkAdapter;
import com.ciko.code.adapter.ResListReviewAdapter;
import com.ciko.code.base.BaseListFragment;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.GetOverTimeFlowsBySelfEty;
import com.ciko.code.http.Req.GetOverTimeFlowsBySelfReq;
import com.ciko.code.http.Req.GetTripFlowsBySelfEty;
import com.ciko.code.http.Req.GetTripFlowsBySelfReq;
import com.ciko.code.type.ResLeaveEnum;
import com.ciko.code.utils.Util;
import com.humanres.activity.ResLeaveDetailActivity;
import com.humanres.activity.ResOutDetailActivity;
import com.humanres.activity.ResOverWorkDetailActivity;
import com.humanres.activity.ResReviewDetailActivity;
import com.humanres.http.Req.GetLeaveFlowsBySelfEty;
import com.humanres.http.Req.GetLeaveFlowsBySelfReq;
import com.mywork.R;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by ciko on 2015/12/9.
 */
@SuppressLint ("ValidFragment")
public class ResMoreSendFragment
        extends
        BaseListFragment
{
	
	private List<GetLeaveFlowsBySelfEty>	getLeaveFlowsBySelfEtyList	   = new ArrayList<>();
	private List<GetTripFlowsBySelfEty>		getTripFlowsBySelfEtyArrayList = new ArrayList<>();
	private List<GetOverTimeFlowsBySelfEty>	getOverTimeFlowsBySelfEtyList  = new ArrayList<>();
																		   
	private ResListLeaveAdapter				resListAdapterLeave;
	private ResListOutAdapter				resListOutAdapter;
	private ResListOverWorkAdapter			resListOverWorkAdapter;
	private ResListReviewAdapter			resListReviewAdapter;
											
	private String							title;
	private TextView						tvTip;
											
	@Override
	protected void initTopBar() {
		unableNav();
	}
	
	@Override
	public void setArguments(Bundle args) {
		super.setArguments(args);
	}
	
	@SuppressLint ("ValidFragment")
	public ResMoreSendFragment(String title) {
		this.title = title;
	}
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser)
		{
			//相当于Fragment的onResume
			if (TextUtils.equals(title, "请假管理"))
			{
				doRequestHttp(new GetLeaveFlowsBySelfReq("0"));
			}
			else if (TextUtils.equals(title, "出差管理"))
			{
				doRequestHttp(new GetTripFlowsBySelfReq("0"));
			}
			else if (TextUtils.equals(title, "加班管理"))
			{
				doRequestHttp(new GetOverTimeFlowsBySelfReq("0"));
			}
		}
		else
		{
			//相当于Fragment的onPause
		}
	}
	
	@Override
	protected void initAdapter() {
		if (TextUtils.equals(title, "请假管理"))
		{
			resListAdapterLeave = new ResListLeaveAdapter(0, getContext(), getLeaveFlowsBySelfEtyList, R.layout.item_list_layout_res_leave);
			setAdapter(resListAdapterLeave);
			doRequestHttp(new GetLeaveFlowsBySelfReq("0"));
			tvTip.setText("暂无请假申请");
		}
		
		if (TextUtils.equals(title, "出差管理"))
		{
			resListOutAdapter = new ResListOutAdapter(0, getContext(), ResLeaveEnum.Send, getTripFlowsBySelfEtyArrayList, R.layout.item_list_layout_res_out);
			setAdapter(resListOutAdapter);
			doRequestHttp(new GetTripFlowsBySelfReq("0"));
			tvTip.setText("暂无出差申请");
		}
		
		if (TextUtils.equals(title, "加班管理"))
		{
			resListOverWorkAdapter = new ResListOverWorkAdapter(0, getContext(), getOverTimeFlowsBySelfEtyList, R.layout.item_list_layout_res_over_work);
			setAdapter(resListOverWorkAdapter);
			doRequestHttp(new GetOverTimeFlowsBySelfReq("0"));
			tvTip.setText("暂无加班申请");
		}
		
		if (TextUtils.equals(title, "报销审批"))
		{
			progressDialog.dismiss();
			List<Integer> list = new ArrayList<Integer>();
			list.add(1);
			list.add(1);
			list.add(1);
			list.add(1);
			list.add(1);
			list.add(1);
			list.add(1);
			list.add(1);
			resListReviewAdapter = new ResListReviewAdapter(getContext(), list, R.layout.item_list_layout_res_review);
			setAdapter(resListReviewAdapter);
		}
		
		//        getLeaveFlowsBySelfEtyList.add(new GetLeaveFlowsBySelfEty());
		//        getLeaveFlowsBySelfEtyList.add(new GetLeaveFlowsBySelfEty());
		//        getLeaveFlowsBySelfEtyList.add(new GetLeaveFlowsBySelfEty());
		//        getLeaveFlowsBySelfEtyList.add(new GetLeaveFlowsBySelfEty());
		//        getLeaveFlowsBySelfEtyList.add(new GetLeaveFlowsBySelfEty());
		//
		//        resListAdapter = new ResListLeaveAdapter(getContext(), getLeaveFlowsBySelfEtyList, R.layout.item_list_layout_res_leave);
		//        setAdapter(resListAdapter);
		
	}
	
	@Override
	protected void initView() {
		super.initView();
		tvTip = findViewById(R.id.tv_tip);
		progressDialog.show();
	}
	
	public void relvef() {
		progressDialog.show();
		if (TextUtils.equals(title, "请假管理"))
		{
			getLeaveFlowsBySelfEtyList.clear();
			resListAdapterLeave.notifyDataSetChanged();
			doRequestHttp(new GetLeaveFlowsBySelfReq("0"));
		}
		
		if (TextUtils.equals(title, "出差管理"))
		{
			getTripFlowsBySelfEtyArrayList.clear();
			resListOutAdapter.notifyDataSetChanged();
			doRequestHttp(new GetTripFlowsBySelfReq("0"));
		}
		
		if (TextUtils.equals(title, "加班管理"))
		{
			getOverTimeFlowsBySelfEtyList.clear();
			resListOverWorkAdapter.notifyDataSetChanged();
			doRequestHttp(new GetOverTimeFlowsBySelfReq("0"));
		}
	}
	
	@Override
	protected void initDates() {
	
	}
	
	@Override
	protected void initHttpRequest() {
	
	}
	
	@Override
	protected void initListener() {
	
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		super.onItemClick(parent, view, position, id);
		
		if (parent.getAdapter() == resListAdapterLeave)
		{
			GetLeaveFlowsBySelfEty getLeaveFlowsBySelfEty = (GetLeaveFlowsBySelfEty) parent.getAdapter().getItem(position);
			Intent intent = new Intent(getContext(), ResLeaveDetailActivity.class);
			intent.putExtra("GetLeaveFlowsBySelfEty", getLeaveFlowsBySelfEty);
			intent.putExtra("fromType", ResLeaveEnum.Send);
			getContext().startActivity(intent);
		}
		if (parent.getAdapter() == resListOutAdapter)
		{
			GetTripFlowsBySelfEty getTripFlowsBySelfEty = (GetTripFlowsBySelfEty) parent.getAdapter().getItem(position);
			Intent intent = new Intent(getContext(), ResOutDetailActivity.class);
			intent.putExtra("GetTripFlowsBySelfEty", getTripFlowsBySelfEty);
			intent.putExtra("fromType", ResLeaveEnum.Send);
			getContext().startActivity(intent);
		}
		if (parent.getAdapter() == resListOverWorkAdapter)
		{
			GetOverTimeFlowsBySelfEty getOverTimeFlowsBySelfEty = (GetOverTimeFlowsBySelfEty) parent.getAdapter().getItem(position);
			Intent intent = new Intent(getContext(), ResOverWorkDetailActivity.class);
			intent.putExtra("GetOverTimeFlowsBySelfEty", getOverTimeFlowsBySelfEty);
			intent.putExtra("fromType", ResLeaveEnum.Send);
			getContext().startActivity(intent);
		}
		if (parent.getAdapter() == resListReviewAdapter)
		{
			//            GetOverTimeFlowsBySelfEty getOverTimeFlowsBySelfEty = (GetOverTimeFlowsBySelfEty) parent.getAdapter().getItem(position);
			//            Intent intent = new Intent(getContext(), ResOverWorkDetailActivity.class);
			//            intent.putExtra("GetOverTimeFlowsBySelfEty", getOverTimeFlowsBySelfEty);
			//            getContext().startActivity(intent);
			intent2Acticity(ResReviewDetailActivity.class);
		}
		
		//        intent2Acticity(ResLeaveDetailActivity.class);
	}
	
	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		Log.e("ResponseSuccess", tag + "");
		super.onResponseSuccess(response, tag);
		progressDialog.dismiss();
		
		switch (tag) {
			case "GetLeaveFlowsBySelfReq":
				List<GetLeaveFlowsBySelfEty> getLeaveFlowsBySelfEtyListResult = (List<GetLeaveFlowsBySelfEty>) response.getReturnObject();
				getLeaveFlowsBySelfEtyList.clear();
				getLeaveFlowsBySelfEtyList.addAll(getLeaveFlowsBySelfEtyListResult);
				resListAdapterLeave.notifyDataSetChanged();
				if (getLeaveFlowsBySelfEtyList.size() == 0)
				{
					findViewById(R.id.lvBaseActivity).setVisibility(View.GONE);
					findViewById(R.id.rl_none_tip_logo).setVisibility(View.VISIBLE);
				}
				
				else
				{
					findViewById(R.id.lvBaseActivity).setVisibility(View.VISIBLE);
					findViewById(R.id.rl_none_tip_logo).setVisibility(View.GONE);
				}
				
				break;
			case "GetTripFlowsBySelfReq":
				List<GetTripFlowsBySelfEty> getTripFlowsBySelfEtyArrayListResult = (List<GetTripFlowsBySelfEty>) response.getReturnObject();
				getTripFlowsBySelfEtyArrayList.clear();
				getTripFlowsBySelfEtyArrayList.addAll(getTripFlowsBySelfEtyArrayListResult);
				resListOutAdapter.notifyDataSetChanged();
				if (getTripFlowsBySelfEtyArrayList.size() == 0)
				{
					findViewById(R.id.lvBaseActivity).setVisibility(View.GONE);
					findViewById(R.id.rl_none_tip_logo).setVisibility(View.VISIBLE);
				}
				else
				{
					findViewById(R.id.lvBaseActivity).setVisibility(View.VISIBLE);
					findViewById(R.id.rl_none_tip_logo).setVisibility(View.GONE);
				}
				
				break;
			case "GetOverTimeFlowsBySelfReq":
				List<GetOverTimeFlowsBySelfEty> getOverTimeFlowsBySelfEtyListResult = (List<GetOverTimeFlowsBySelfEty>) response.getReturnObject();
				getOverTimeFlowsBySelfEtyList.clear();
				getOverTimeFlowsBySelfEtyList.addAll(getOverTimeFlowsBySelfEtyListResult);
				resListOverWorkAdapter.notifyDataSetChanged();
				if (getOverTimeFlowsBySelfEtyList.size() == 0)
				{
					findViewById(R.id.lvBaseActivity).setVisibility(View.GONE);
					findViewById(R.id.rl_none_tip_logo).setVisibility(View.VISIBLE);
				}
				else
				{
					findViewById(R.id.lvBaseActivity).setVisibility(View.VISIBLE);
					findViewById(R.id.rl_none_tip_logo).setVisibility(View.GONE);
				}
				break;
		}
		
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		progressDialog.dismiss();
		Util.showToast(getActivity(), "加载失败，请稍后再试");
	}
}
